package cn.iocoder.yudao.module.infra.framework.nacos;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
@Slf4j
public class NacosConfig {

    @Value(value = "${nacos.config.server-addr}")
    private String serverAddr;

    @Value(value = "${nacos.config.username}")
    private String username;

    @Value(value = "${nacos.config.password}")
    private String password;

    @Bean
    public ConfigService injectConfigService() throws NacosException {
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        properties.put("username", username);
        properties.put("password", password);
        log.info("Initializing Nacos ConfigService with serverAddr: {}", serverAddr);

        final ConfigService configService = NacosFactory.createConfigService(properties);
        log.info("Nacos ConfigService initialized successfully.");

        return configService;
    }
}
